<!--  -->
<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav-bar" @titleClick="titleClick" ref="nav"></detail-nav-bar>
    <scroll class="content" ref="scroll" @scroll="contentScroll" :probeType="3">
      <detail-swiper :topImages="topImages"></detail-swiper>
      <detail-base-info :baseInfo="goods"></detail-base-info>
      <detail-shop-info :shopInfo="shop"></detail-shop-info>
      <detail-goods-info :goodsInfo="detailInfo" @goodsInfoImgLoad="goodsInfoImgLoad"></detail-goods-info>
      <detail-goods-params :goodsParams="paramInfo" ref="params"></detail-goods-params>
      <detail-comment-info :commentInfo="commentInfo" ref="comment"></detail-comment-info>
      <goods-list :goodsList="recommendList" ref="recommend"></goods-list>
    </scroll>
    <detail-bottom-bar class="bottom-bar" @addcart="addcart"></detail-bottom-bar>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
    <!-- <toast :message="message" :show="isShow"></toast> -->
  </div>
</template>

<script>
import DetailNavBar from "views/detail/childComps/DetailNavBar";
import DetailSwiper from "views/detail/childComps/DetailSwiper";
import DetailBaseInfo from "views/detail/childComps/DetailBaseInfo";
import DetailShopInfo from "views/detail/childComps/DetailShopInfo";
import DetailGoodsInfo from "views/detail/childComps/DetailGoodsInfo";
import DetailGoodsParams from "views/detail/childComps/DetailGoodsParams";
import DetailCommentInfo from "views/detail/childComps/DetailCommentInfo";
import DetailBottomBar from "views/detail/childComps/DetailBottomBar";

import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll";
import Toast from 'components/common/toast/Toast';

import { getDetail, GoodsInfo, Shop, getRecommend } from "network/detail.js";
import { itemListenerMixin, backTopMixin } from "common/mixin.js";
import { debounce } from "common/utils.js";

export default {
  name: "Detail",
  data() {
    return {
      iid: "",
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommendList: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0,
      message: '添加购物车成功',
      isShow: false
    };
  },
  mixins: [itemListenerMixin, backTopMixin],
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailGoodsParams,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    Toast
  },
  created() {
    this.iid = this.$route.params.iid;

    getDetail(this.iid).then((res) => {
      const data = res.result;
      this.topImages = data.itemInfo.topImages;
      this.goods = new GoodsInfo(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      this.shop = new Shop(data.shopInfo);
      this.detailInfo = data.detailInfo;
      this.paramInfo = data.itemParams;
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }
    });

    getRecommend().then((res) => {
      this.recommendList = res.data.list;
    });

    this.getThemeTopY = debounce(() => {
      this.themeTopYs = [];
      this.themeTopYs.push(0);
      this.themeTopYs.push(-this.$refs.params.$el.offsetTop);
      this.themeTopYs.push(-this.$refs.comment.$el.offsetTop);
      this.themeTopYs.push(-this.$refs.recommend.$el.offsetTop);
      this.themeTopYs.push(-Number.MAX_VALUE);
    });
  },
  methods: {
    goodsInfoImgLoad() {
      this.$refs.scroll && this.$refs.scroll.refresh();
      this.getThemeTopY();
    },
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, this.themeTopYs[index], 100);
    },
    contentScroll(position) {
      const positionY = -position.y;
      let length = this.themeTopYs.length;
      for (let i = 0; i < length - 1; i++) {
        // if (this.currentIndex !== i
        //   && ((i < length - 1 && positionY >= -this.themeTopYs[i] && positionY < -this.themeTopYs[i + 1])
        //   || (i === length - 1 && positionY >= -this.themeTopYs[i]))) {
        //     this.currentIndex = i;
        //     this.$refs.nav.currentIndex = this.currentIndex;
        //   }
        if (
          this.currentIndex !== i &&
          positionY > -this.themeTopYs[i] &&
          positionY < -this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }

      this.isShowBackTop = Math.abs(position.y) > 1000;
    },
    addcart() {
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.newPrice;
      product.realPrice = this.goods.realPrice;
      product.iid = this.iid;
      product.isSelected = true;
      product.count = 1;

      this.$store.commit("addCart", product);

      // this.isShow = true;
      // setTimeout(() => {
      //   this.isShow = false;
      // }, 1500);
      this.$toast.show('添加购物车成功', 1500);

    },
  },
  destroyed() {
    this.$bus.$off("imageLoad", this.itemImgListener);
  },
};
</script>
<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}

.content {
  height: calc(100% - 44px);
}

.detail-nav-bar {
  position: relative;
  z-index: 9;
  background-color: #fff;
}
</style>